package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbStep;
import com.akk.module.avs.model.request.AvsYbStepRequest;
import com.akk.module.avs.service.AvsYbLoanStepService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class YbStepBusiness {

    @Autowired
    private AvsYbLoanStepService loanStepService;

    @Autowired
    private PictureService pictureService;

    public RestResponse list(Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        PageHelper.startPage(query.getPage(),query.getLimit());
        Map params = query.getSearch();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        List<AvsYbStep> list = loanStepService.queryByParams(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse save(AvsYbStepRequest request,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        Map result = pictureService.uploadPicture(request.getIcon());
        if (result.get("status").toString().equals("1"))
            return RestResponse.build("1","文件上传失败");
        request.setStepIcon(result.get("url").toString());
        request.setAppId(ShiroUtils.getAdmin().getAppId());
        loanStepService.save(request);
        return RestResponse.build("success");
    }

    public RestResponse update(AvsYbStepRequest request,BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        if (request.getIcon() != null){
            Map result = pictureService.uploadPicture(request.getIcon());
            if (result.get("status").toString().equals(1))
                return RestResponse.build("1","文件上传失败.");
            request.setStepIcon(result.get("url").toString());
        }
        loanStepService.update(request);
        return RestResponse.build("success");
    }

    public RestResponse del(Long stepId){
        loanStepService.del(stepId);
        return RestResponse.build("success");
    }

    public RestResponse stepAll() {
        Map params = new HashMap();
        params.put("appId",ShiroUtils.getAdmin().getAppId());
        List<AvsYbStep> list = loanStepService.queryByParams(params);
        return RestResponse.build(list);
    }
}
